package com.etime.shycourse.controller.accountonline;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.dto.SettlementDto;
import com.etime.shycourse.pojo.Settlement;
import com.etime.shycourse.services.account.interfaces.AccountMyOrgService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author liukang
 * @Date 2022/7/26 14:26
 */
@Api(tags = "机构-我的提成")
@RestController
@RequestMapping("/sys/account/org/my")
public class AccountMyOrgController {
    @Autowired
    AccountMyOrgService accountMyOrgService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("settlements")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="token 传递字符串"),
            @ApiImplicitParam(name = "pagenum",value = "第几页"),
            @ApiImplicitParam(name = "pagesize",value = "每页多少行"),
            @ApiImplicitParam(name="settlementpay",value="是否支付 传递数字 0代表未支付"),

    })
    @GetMapping("/settlements")
    public PageMessage<SettlementDto> getOrgSettlements(@RequestHeader("token") String token, long pagenum, long pagesize, int settlementpay) {
        System.out.println(token);
        String userinfo = stringRedisTemplate.opsForValue().get(token);
        System.out.println(userinfo);
        ObjectMapper om = new ObjectMapper();
        CurrentUser currentUser=null;
        try {
             currentUser = om.readValue(userinfo, CurrentUser.class);
            System.out.println(currentUser);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        PageMessage<SettlementDto> settlementDtoPageMessage = new PageMessage<>();
        Page<SettlementDto> orgSettlements = accountMyOrgService.getOrgSettlements(pagenum, pagesize, settlementpay,currentUser.getOrgid());
        settlementDtoPageMessage.setPagedata(orgSettlements);
        settlementDtoPageMessage.setCode(1);
        return settlementDtoPageMessage;
    }

    @ApiOperation("获取机构提成")
    @GetMapping("/settlementDetails")
    public PageMessage<SettlementDto> getOrgSettlementDetails(long pagenum, long pagesize, int settlementpay, int orgid) {
        PageMessage<SettlementDto> settlementDtoPageMessage = new PageMessage<>();
        Page<SettlementDto> orgSettlements = accountMyOrgService.getOrgSettlementDetails(pagenum, pagesize, settlementpay, orgid);
        settlementDtoPageMessage.setPagedata(orgSettlements);
        settlementDtoPageMessage.setCode(1);
        return settlementDtoPageMessage;
    }


    @ApiOperation("查询日期列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
            @ApiImplicitParam(name = "settlementpay", value = "是否支付"),
            @ApiImplicitParam(name = "userid", value = "用户id"),

    })
    @GetMapping("/dateList")
    public PageMessage<Date> findDateList(long pagenum, long pagesize, int settlementpay, Integer orgid) {
        PageMessage<Date> msg = new PageMessage<>();
        Page<Date> settlementDtoList = accountMyOrgService.findDateList(pagenum, pagesize, settlementpay, orgid);
        msg.setPagedata(settlementDtoList);
        msg.setCode(1);
        return msg;
    }

    /*
     * 通过日期获取settlement结算信息
     * */
    @ApiOperation("通过日期获取settlement结算信息")
    @GetMapping("/getSettlementByDate")
    public ListMessage<SettlementDto> getSettlementByDate(int settlementpay, String settlementtime, int orgid) {
        ListMessage<SettlementDto> msg = new ListMessage<>();
        List<SettlementDto> settlementDtoPage = accountMyOrgService.getSettlementByDate(settlementpay, settlementtime, orgid);
        msg.setDatalist(settlementDtoPage);
        msg.setCode(1);
        return msg;
    }
}
